package com.category.douyijia.utils;

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
/**
 * internet server address manager's class.
 * it can get  server host name,
 * host ipV4 address ,
 * host ipV6 address and
 * host MAC address.
 * @author JunfengLiu
 * @version v1.0
 * date time: 2021-7-17 18:35:23
 */
/* 
 * 服务器网络连接地址管理类。
 * 用来获取服务器主机名称，
 * 服务器ip地址，
 * 服务器MAC地址。
 * @author 刘俊沣
 * @version v1.0
 * 时间: 2021-7-17 18:35:23
 */
public class ServerSocket {
	/**
	 * saved the server name.
	 */
	/*
	 * 服务器名称
	 */
	private static String serverName;
	/**
	 * saved the server ipv4 address.
	 */
	/*
	 * 服务器IPV4地址
	 */
	private static String serverIpV4Address;
	/**
	 * saved the ipv6 address.
	 */
	/*
	 * 服务器IPv6地址
	 */
	private static String serverIpV6Address;
	/**
	 * saved the MAC address.
	 */
	/*
	 * 服务器MAC地址
	 */
	private static String serverMACAddress;
	/**
	 * create elements saved the MAC address unit string.
	 */
	/*
	 * 创建截取MAC地址的元素组。
	 */
	private static Enumeration<NetworkInterface> el;
	/**
	 * get the server host name.
	 * @return
	 * a type of string's server host name.
	 * @throws UnknownHostException
	 * if not find the host name throw this exception.
	 */
	/* 
	 * 获取服务器主机名称
	 * @return
	 * 返回字符串类型的服务器主机名称
	 * @throws UnknownHostException
	 * 抛出异常，未找到主机
	 */
	public static String getHostName() throws UnknownHostException  {
		return serverName = InetAddress.getLocalHost().getHostName().toString();
	}
	/**
	 * get the server host ipv4 address.
	 * @return
	 * a type of string's server host ipv4 address.
	 * @throws UnknownHostException
	 * if not find the host ipv4 address throw exception.
	 */
	/* 
	 * 获取服务器主机IPv4地址
	 * @return
	 * 获取字符串类型的IPv4地址
	 * @throws UnknownHostException
	 * 抛出异常，找不到主机
	 */
	public static String getHostIpV4Address() throws UnknownHostException {
		return serverIpV4Address = InetAddress
									.getLocalHost()
									.getHostAddress()
									.toString();
	}
	/**
	 * get the server host ipv6 address.
	 * @return
	 * a type of string's server host ipv6 address.
	 * @throws UnknownHostException
	 * if not find the host ipv6 address throw exception.
	 */
	/* 
	 * 获取服务器主机IPv6地址（已过时）
	 * @return
	 * 返回字符串类型的IPv6地址
	 * @throws UnknownHostException
	 * 抛出异常，找不到主机
	 */
	@Deprecated
	public static String getHostIpV6Address() throws UnknownHostException {
		return serverIpV6Address = Inet6Address
									.getLocalHost()
									.getHostAddress();
	}
	/**
	 * get the server host MAC address.
	 * @return
	 * a type of string's server host MAC address.
	 * @throws SocketException
	 * if not find the host MAC address throw exception.
	 */
	/* 
	 * 获取服务器主机的MAC地址
	 * @return
	 * 返回服务器主机的MAC地主
	 * @throws SocketException
	 * 抛出异常，找不到主机
	 */
	public static String getHostMACAddress() throws SocketException {
		//获取元素组接口
		el = NetworkInterface.getNetworkInterfaces();
		//循环遍历元素组
		while (el.hasMoreElements()) {
			//拆分成byte类型的数组
        	byte[] mac = el.nextElement().getHardwareAddress();
        	//判断获取到的值是否为空，如果为空跳过
        	if (mac == null){
        		continue;
        	}
        	//进行MAC地址解析
        	serverMACAddress = hexByte(mac[0]) 
        				+ "-" + hexByte(mac[1]) + "-"
        				+ hexByte(mac[2]) + "-" + hexByte(mac[3]) + "-"
        				+ hexByte(mac[4]) + "-" + hexByte(mac[5]);
         }
		return serverMACAddress;
	}
	/**
	 * get the number convert to string 
	 * and subset the string. 
	 * @param b
	 * address number's group.
	 * @return
	 * the type of string's number.
	 */
	/* 
	 * 获取并转换字符串数组，
	 * 对每一个元素进行截取。
	 * @param b
	 * 地址编号组
	 * @return
	 * 字符串型的地址编号
	 */
	private static String hexByte(byte b) {
		//设置16进制字符串
        String s = "000000" + Integer.toHexString(b);
        //截取字符串，长度为字符串长度-2
        return s.substring(s.length() - 2);
    }
//	/**
//	 * test this methods,
//	 * and update the methods
//	 * and test.
//	 * @param args
//	 */
//	/*
//	 * 测试这些方法，
//	 * 并对方法进行更新
//	 * @param args
//	 */
//	public static void main(String[] args) {
//		try {
//			System.out.println("主机名："+ServerSocket.getHostName());
//			System.out.println("ipv4地址："+ServerSocket.getHostIpV4Address());
//			System.out.println("ipv6地址："+ServerSocket.getHostIpV6Address());
//			System.out.println("MAC地址："+ServerSocket.getHostMACAddress());
//		} catch (UnknownHostException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}catch (SocketException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
}